package com.handpay.zztong.hp.swiper;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.swiper.ApiSwiperFactory;
import com.handpay.framework.swiper.SwiperModel;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.log.HPLog;

/**
 * 保存全局刷卡器的状态。
 *
 * @author jmshuai
 */
public class SwiperUtils {
    // 刷卡器合法性检测程序（1、2、3、4）
    public static boolean isVoiceIn() {
        Boolean b = (Boolean) ClientEngine.getInstance().getGlobal(ClientEngine.VOICE_PLUGIN, Boolean.FALSE);
        return b.booleanValue();
    }

    /**
     * 1、刷卡器是否插入
     *
     * @return
     */
    private static final String SwiperOn = "SwiperOn";            // 刷卡器已插入
    private static final String CSN = "CSN";                // CSN，刷卡器通讯成功
    /**
     * 0：可用;1：未绑定;2：已绑定到其他用户;3：已绑定到其他渠道;4：挂失;5：注销
     */
    private static final String SwiperStatus = "SwiperStatus";        // 刷卡器业务状态
    private static final String SwiperDes = "SwiperDescription";    // 刷卡器业务状态
    private static final String BandSwiperStatus = "BandSwiperStatus";    // 刷卡器绑定状态

    /**
     * 保存csn。
     *
     * @param csn 如果csn为null。表示清除csn
     */
    public static void saveCsn(String csn) {
        // 如果清空csn。同时也要清空刷卡器的状态
        //保存csn的时候，都要清空刷卡器状态。而不仅仅是清空csn的时候。
        //譬如，
        //if (TextUtils.isEmpty(csn))
        //{
        setSwiperStatus("", "");
        //}
        ClientEngine.getInstance().saveGlobal(CSN, csn);
    }

    /**
     * 获取csn
     *
     * @return
     */
    public static String getCsn() {
        Object csn = ClientEngine.getInstance().getGlobal(CSN);
        return null == csn ? null : (String) csn;
    }

    /**
     * 设置音频口状态
     *
     * @param on
     */
    public static void setSwiperStatus(boolean on) {
        ClientEngine.getInstance().saveGlobal(SwiperOn, on);
    }

    /**
     * 判断音频口是否插入
     *
     * @return true表示已经插入
     */
    public static boolean isSwiperConnected() {
        boolean isSwiperOn = (Boolean) ClientEngine.getInstance().getGlobal(SwiperOn, false);
        return isSwiperOn;
    }

    /**
     * 设置刷卡器状态
     *
     * @param status
     */
    public static void setSwiperStatus(String status, String description) {
        HPLog.i(HPLog.YL_TAG, "setSwiperStatus:" + status);
        ClientEngine.getInstance().saveGlobal(SwiperStatus, status);
        ClientEngine.getInstance().saveGlobal(SwiperDes, description);
    }

    /**
     * 设置刷卡器状态
     */
    public static void setSwiperSuccess() {
        HPLog.i(HPLog.YL_TAG, "setSwiperStatus:0");
        ClientEngine.getInstance().saveGlobal(SwiperStatus, "0");
        ClientEngine.getInstance().saveGlobal(SwiperDes, "");
    }

    /**
     * 2、刷卡器能否通讯，读取CSN
     *
     * @return
     */
    public static boolean isSwiperOK() {
        String csn = (String) ClientEngine.getInstance().getGlobal(CSN);
        return !TextUtils.isEmpty(csn);
    }

    /**
     * 3、是否需要查询刷卡器状态
     *
     * @return
     */
    public static boolean isNeedGetSwiperStatus() {
        String swiperstatus = (String) ClientEngine.getInstance().getGlobal(SwiperStatus);
        return TextUtils.isEmpty(swiperstatus);
    }

    /**
     * 4、刷卡器是否需要绑定
     *
     * @return
     */
    public static boolean canBind() {
        String ss = (String) ClientEngine.getInstance().getGlobal(SwiperStatus);
        return "1".equals(ss);
    }

    /**
     * 判断当前刷卡器是不是绑定当前账户下的。是返回true
     *
     * @return
     */
    public static boolean isAccountSwiper() {
        String ss = (String) ClientEngine.getInstance().getGlobal(SwiperStatus);

        return "0".equals(ss);
    }

    public static String getSwiperDes() {
        Object des = ClientEngine.getInstance().getGlobal(SwiperDes);
        return null == des ? null : (String) des;
    }

    /**
     * 判断是否只支持一个刷卡器
     * @param c
     * @return true表示只支持一个刷卡器

    public static boolean isOnlySupportOneSwiper(Context c) {
    if (null != c) {
    int[] models = c.getResources().getIntArray(R.array.swipe_model);
    if (1 == models.length) {
    return true;
    } else {
    return false;
    }
    }
    return false;
    }*/

    /**
     * 判断是不是存在支持打印的刷卡器
     *
     * @param c
     * @return true表示存在支持打印的刷卡器
     */
    public static boolean hasPrintSupportSwiper(Context c) {
        if (null != c) {
            List<int[]> modles = new ArrayList<int[]>();
            int[] voice_models = c.getResources().getIntArray(R.array.voice_swiper_type);
            int[] bluetooth_models = c.getResources().getIntArray(R.array.bluetooth_swiper_type);
            modles.add(voice_models);
            modles.add(bluetooth_models);
            modles.toArray();
            for (int[] pos : modles) {
                for (int div : pos) {
                    SwiperModel swiperModel = SwiperModel.values()[div];
                    if (swiperModel.mSupportPrint && SwiperModel.ME31_BLUETOOTH == ApiSwiperFactory.getInstance().getSwiperModel()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private static final String BT_MAC_KEY = "bluetooth_mac";

    /**
     * 保存用户选择连接的蓝牙设备的add
     *
     * @param add 为空时，表示清除
     */
    public static void saveConnectedBTMac(String add) {
        ClientEngine.getInstance().saveGlobal(BT_MAC_KEY, add);
    }

    /**
     * @return 返回用户选择的蓝牙设备的add，不可能为null
     */
    public static String getConnectedBTMac() {
        return (String) ClientEngine.getInstance().getGlobal(BT_MAC_KEY, "");
    }

    /**
     * 清楚所有缓存
     */
    public static void cleanSwiperData() {
        saveConnectedBTMac(null);
        setSwiperStatus("", "");
        saveCsn(null);
        setSwiperStatus(false);
        ApiSwiperFactory.getInstance().clearSwiper(null);


    }

    /**
     * 设置是否需要检测刷卡器可用
     *
     * @param on
     */
    public static void setBandSwiperStatus(boolean on) {
        ClientEngine.getInstance().saveGlobal(BandSwiperStatus, on);
    }

    public static boolean gettBandSwiperStatus() {
        Object des = ClientEngine.getInstance().getGlobal(BandSwiperStatus);
        if (null == des) {
            return false;
        }
        if (des instanceof Boolean) {
            return (Boolean) des;
        }
        return false;
    }

}
